/**
 * 
 */
package cn.panda.edu.teacher;

import cn.panda.edu.model.Notice;
import cn.panda.edu.model.Section;
import cn.panda.edu.model.Teacher;
import cn.panda.edu.service.NoticeService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.ResultDataUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**  
 * 类描述：  
 */
@RestController
@RequestMapping("/api/teacher/notice")
public class TeacherNoticeController {
	
	//每页显示条数
	final int PAGESIZE=10;
	
	@Autowired
	private NoticeService noticeService;
	
	/**
	 * 
	* @Title: getNoticeById 
	* @Description: 根据id查询公告内容
	*@return  
	* Notice
	 */
	@ApiOperation(value = "获取公告详细内容")
	@ApiImplicitParam(name = "id", value = "当前公告id", required = true, dataType = "int", paramType = "form")
	@RequestMapping(value = "/getNoticeById", method = RequestMethod.POST)
	@ResponseBody
	public Object getNoticeById(Integer id) {
		return ResultDataUtil.resData(200, "", noticeService.selectByPrimaryKey(id));
	}

	/**
	 * 
	* @Title: getList 
	* @Description: 查询所有公告
	*@return  
	* List<Notice>
	 */
	@ApiOperation(value="公告列表")
	@RequestMapping(value="/list",method= RequestMethod.POST)
	 @ApiImplicitParam(name = "pageIndex", value = "当前页数", required = true, dataType = "Integer")
	public Object getList(Integer pageIndex) {
		if(pageIndex==null) {
			pageIndex=1;
		}
		PageInfo<Notice> pageInfo=noticeService.getAllNotice(pageIndex, Constant.PAGE_SIZE);
		return ResultDataUtil.resData(200,"", pageInfo);
	}



	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public  Object insert(@RequestBody Notice notice, HttpSession session) {
		int status=200;
		String message=null;
		try {
			Teacher teacher= (Teacher) session.getAttribute("teacher");
			notice.setTeacherId(teacher.getTeacherId());
			notice.setNoticeCreateDate(new Date());
			noticeService.insertSelective(notice);
			status=200;
			message="添加成功!";
		} catch (Exception e) {
			status=500;
			message="添加失败!";
		}
		return ResultDataUtil.resData(status, message, null);
	}
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public  Object update(@RequestBody Notice notice) {
		int status=200;
		String message=null;
		try {
			noticeService.updateByPrimaryKeySelective(notice);
			status=200;
			message="修改成功!";
		} catch (Exception e) {
			status=500;
			message="修改失败!";
		}
		return ResultDataUtil.resData(status, message, null);
	}


	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public  Object update(Integer id) {
		int status=200;
		String message=null;
		try {
			noticeService.deleteByPrimaryKey(id);
			status=200;
			message="删除成功!";
		} catch (Exception e) {
			status=500;
			message="删除失败!";
		}
		return ResultDataUtil.resData(status, message, null);
	}

}
